<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <label class="el-form-item-label">名称</label>
        <el-select
          v-model="query.cId"
          clearable
          style="width: 185px;"
          placeholder="名称"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        >
          <el-option :value="item.id" :label="item.name"  v-for="item in allCabinets" :key="item.id"/>
        </el-select>
        <label class="el-form-item-label">类型</label>
        <el-select
          v-model="query.type"
          clearable
          style="width: 185px;"
          placeholder="类型"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        >
          <el-option value="1" label="试剂柜" />
          <el-option value="2" label="毒品柜" />
          <el-option value="3" label="冰箱" />
          <el-option value="4" label="培养箱" />
        </el-select>
        <rrOperation :crud="crud" />
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" />
      <!--表单组件-->
      <el-dialog
        :close-on-click-modal="false"
        :before-close="crud.cancelCU"
        :visible.sync="crud.status.cu > 0"
        :title="crud.status.title"
        width="500px"
      >
        <el-form
          ref="form"
          :model="form"
          :rules="rules"
          size="small"
          label-width="80px"
        />
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button
            :loading="crud.status.cu === 2"
            type="primary"
            @click="crud.submitCU"
          >确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table
        ref="table"
        v-loading="crud.loading"
        :data="crud.data"
        size="small"
        style="width: 100%;"
        @selection-change="crud.selectionChangeHandler"
      >
        <!-- <el-table-column type="selection" width="55" /> -->
        <el-table-column prop="cabinetName" label="名称"  align="center"/>
        <el-table-column prop="type" label="类型" align="center">
          <template slot-scope="scope">
            <span v-if="scope.row.type == 1">试剂柜</span>
            <span v-if="scope.row.type == 2">毒品柜</span>
            <span v-if="scope.row.type == 3">冰箱</span>
            <span v-if="scope.row.type == 4">培养箱</span>
          </template>
        </el-table-column>
        <el-table-column prop="userName" label="操作人"  align="center"/>
        <el-table-column prop="createdAt" label="时间"  align="center"/>
        <el-table-column prop="isOpen" label="开/关门记录" align="center">
          <template slot-scope="scope">
            <el-tag type="success" v-if="scope.row.isOpen == 1">开门记录</el-tag>
            <el-tag type="danger" v-if="scope.row.isOpen == 0">关门记录</el-tag>
          </template>
        </el-table-column>
        <!-- <el-table-column v-if="checkPer(['admin','sensorLog:edit','sensorLog:del'])" label="操作" width="150px" align="center">
          <template slot-scope="scope">
            <udOperation
              :data="scope.row"
              :permission="permission"
            />
          </template>
        </el-table-column> -->
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import { getAllCabinetsAPI } from '@/api/cabinet'
import crudSensorLog, { searchIdSensorLogAPI }  from '@/api/sensorLog'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
import { mapState } from 'vuex'

const defaultForm = {
  id: null,
  sensorId: null,
  data: null,
  createdAt: null,
  updatedAt: null,
  isDeleted: null,
  type: null
}
export default {
  name: 'SensorLogVoc',
  components: { pagination, crudOperation, rrOperation, udOperation },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  cruds() {
    return CRUD({
      title: '开关门记录',
      url: 'api/opendoorrecord',
      idField: 'id',
      sort: 'id,desc',
      crudMethod: { ...crudSensorLog },
      optShow: {
      add: false,
      edit: false,
      del: false,
      download: true,
      reset: true
    },
    })
  },
   computed: {
    ...mapState('hjuser', ['systemSearchInfoId'])
  },
  data() {
    return {
      permission: {
        add: ['admin', 'sensorLog:add'],
        edit: ['admin', 'sensorLog:edit'],
        del: ['admin', 'sensorLog:del']
      },
      rules: {},
      queryTypeOptions: [
        { key: 'sensorId', display_name: '名称' },
        { key: 'type', display_name: '类型' }
      ],
      allCabinets:[]
    }
  },
  created(){
    this.getAllCabinets()
  },
  methods: {
    async getAllCabinets (){
      const res = await getAllCabinetsAPI()
      this.allCabinets = res
    },
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
     async [CRUD.HOOK.afterRefresh]() {
      if (this.systemSearchInfoId != '') {
        try {
          const res = await searchIdSensorLogAPI(this.systemSearchInfoId,5)
          this.crud.data = res.content
        } finally {
          this.$store.commit('hjuser/updatedsystemSearchInfoId', '')
        }
      }
      return true
    },
  }
}
</script>

<style scoped></style>
